<?php

require 'vars.php';
require 'config.mysql.php';
require 'functs.php';

$link = MySQL_go($mysql_server,$mysql_user,$mysql_pass);
MySQL_set_db($link,$mysql_db);

$email_enablesql = "SELECT `email_enable` FROM `featherchat_settings";
$result = mysql_query($email_enablesql) or die ("Unable to get email status: ".mysql_error());
$email_enable = mysql_result($result,0);

Page_startup($title,$version);

if ($email_enable == "1") {
	if (empty($_POST['email'])) { ?>
		To reset your password, please enter the e-mail address you registered with:<br />
		<form method="post" action="resetpass.php">
			<input type="text" size="30" name="email" />
			<input type="submit" value="Go" />
		</form>
		<?php
	}
	else {

		$email = mysql_real_escape_string($_POST[email]);
	
		$usernamesql = "SELECT `name` FROM `featherchat_users` WHERE `email` = '".$email."'";
		$result = mysql_query($usernamesql);
		$username = @mysql_result($result,0) or die('No user found with the provided address.'.mysql_error());
		
		$email_configsql = "SELECT `email_user`,`email_pass`,`email_host`,`email_port`,`featherchataddr` FROM `featherchat_settings`";
		$result = mysql_query($email_configsql) or die ('Unable to get e-mail info.'.mysql_error());
		$email_config = mysql_fetch_assoc($result);

		if (!empty($username)) { Reset_password($email_config,$email); }
		Home_link();
	}
}
else {
	echo "Your admin has not configured e-mail notifications, which are required to reset a password.<br />"; 
	Home_link();
}

Page_shutdown();
MySQL_halt($link);